package com.app.common.utils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Map;

import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;

import com.css.sword.platform.comm.json.JSON;
import com.css.sword.platform.web.event.IReqData;
import com.css.sword.platform.web.event.SwordReq;

public class ReqJsonUtil {
	
	/**
	 * 将指定的json串转为Map对象
	 * @param reqJson 指定的json字符串
	 * @return 转化后的Map对象
	 */
	@SuppressWarnings("unchecked")
	public static Map<String,Object> json2map(String reqJson){
		
		ObjectMapper mapper=new ObjectMapper();		
		Map<String, Object> reqMap = null;
		Map<String, Object> postMap = null;
		try {
			reqMap = (Map<String, Object>) mapper.readValue(reqJson,Object.class);
			postMap = (Map<String, Object>) reqMap.get("dataMap");
		} catch (JsonParseException e) {
			e.printStackTrace();
		} catch (JsonMappingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		return postMap;
	}
	
/*	@SuppressWarnings("unchecked")
	public static Map<String,Object> getPostMap(IReqData req){
		
		//ObjectMapper mapper=new ObjectMapper();		
		Map<String, Object> reqMap = null;
		Map<String, Object> postMap = null;
		try {
			//reqMap = (Map<String, Object>) mapper.readValue(reqJson,Object.class);
			reqMap = JSON.getJsonObject(((SwordReq) req).getReqDataSet().getReqJson());
			postMap = (Map<String, Object>) reqMap.get("dataMap");
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return postMap;
	}*/
	
	/**
	 * 通过req对象获得前台post方式传递过来的参数值
	 * @param req 封装的req对象
	 * @param paramName 指定要获取参数的参数名
	 * @return 参数值
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public static String getPostParam(IReqData req,String paramName){
		
		Map<String, Object> reqMap = null;
		Map<String, Object> postMap = null;
		String paramValue = "";
		try {
			reqMap = JSON.getJsonObject(((SwordReq) req).getReqDataSet().getReqJson());
			postMap = (Map<String, Object>) reqMap.get("dataMap");
			paramValue = StringUtil.parse2String(((ArrayList)postMap.get(paramName)).get(0));
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return paramValue;
	}
}
